package com.enqualcomm.kids.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.view.MyHorizontalScrollView;
import com.enqualcomm.kids.view.MyScrollView;

import common.utils.DensityUtil;
import common.utils.MyFrontUtil;

public class InputHeightAndWeightDialog extends Dialog {
    private InputOverCallBack callback;
    private int currentHeight;
    private int currentWeight;
    private float cell;
    private MyScrollView height_sv;
    private TextView height_number_tv;
    private TextView weight_number_tv;
    private MyHorizontalScrollView weight_hsv;
    private View setHeight_rl;
    private View setWeight_rl;
    private Button left_btn;
    private Button right_btn;
    private TextView titleText;
    private final String cancel;
    private final String next;
    private final String heightStr;
    private final String weightStr;
    private final String ok;
    private final String previous;
    private String watchName;
    private Context context;


    public InputHeightAndWeightDialog(Context context, int height, int weight, String watchName, InputOverCallBack callback) {
        super(context);
        this.context = context;
        this.callback = callback;

        this.currentHeight = height;
        this.currentWeight = weight;

        cancel = context.getString(R.string.cancel);
        next = context.getString(R.string.next);
        heightStr = context.getString(R.string.height);
        weightStr = context.getString(R.string.weight);
        ok = context.getString(R.string.ok);
        previous = context.getString(R.string.previous);
        this.watchName = watchName;
    }

    public interface InputOverCallBack {
        void intputOver(int height, int weight);
    }

    private void initDialogView() {
        cell = transfer(7);//每格是相对7个单位

        titleText = (TextView) findViewById(R.id.titleText);
        setHeight_rl = findViewById(R.id.setHeight_rl);
        setWeight_rl = findViewById(R.id.setWeight_rl);

        initHeightLayout();

        //取消/上一步按钮
        left_btn = (Button) findViewById(R.id.left_btn);
        left_btn.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View arg0, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (cancel.equals(left_btn.getText().toString())) {
                        dismiss();
                    } else {
                        setWeight_rl.setVisibility(View.INVISIBLE);
                        setHeight_rl.setVisibility(View.VISIBLE);
                        left_btn.setText(cancel);
                        right_btn.setText(next);
                        titleText.setText(heightStr);
//                        if (!TextUtils.isEmpty(watchName)) {
//                            MyFrontUtil.initTextViewText(context, titleText, watchName, context.getString(R.string.t_info_height));
//                        }

                    }
                }
                return false;
            }
        });

        //下一步/确定按钮
        right_btn = (Button) findViewById(R.id.right_btn);
        right_btn.setOnTouchListener(new OnTouchListener() {
            public boolean onTouch(View view, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    if (next.equals(right_btn.getText().toString())) {
                        setHeight_rl.setVisibility(View.INVISIBLE);
                        setWeight_rl.setVisibility(View.VISIBLE);
                        initWeightLayout();
                        left_btn.setText(previous);
                        right_btn.setText(ok);
                        titleText.setText(weightStr);
//                        if (!TextUtils.isEmpty(watchName)) {
//                            MyFrontUtil.initTextViewText(context, titleText, watchName, context.getString(R.string.t_info_weight));
//                        }
                    } else {
                        callback.intputOver(currentHeight, currentWeight);
                        dismiss();
                    }
                }
                return false;
            }
        });

    }

    private void initHeightLayout() {
        height_number_tv = (TextView) findViewById(R.id.height_number_tv);
        height_number_tv.setText(String.valueOf(currentHeight));

        View height_iv = findViewById(R.id.height_iv);
        int heightIvPadding = DensityUtil.dip2px(getContext(), 51) - (int) (cell * 5 + 0.5f);
        height_iv.setPadding(0, heightIvPadding, 0, heightIvPadding);

        height_sv = (MyScrollView) findViewById(R.id.height_sv);
        height_sv.post(new Runnable() {
            @Override
            public void run() {
                height_sv.scrollTo(0, (int) (cell * (200 - currentHeight) + 0.5f));
                height_sv.setScrollChangedListener(new MyScrollView.ScrollViewListener() {
                    @Override
                    public void onScrollChanged(MyScrollView scrollView, int x, int y,
                                                int oldx, int oldy) {
                        if (y != oldy) {
                            currentHeight = convertToHeight(y);
                            height_number_tv.setText(String.valueOf(currentHeight));
                        }
                    }
                });
            }
        });
    }


    private void initWeightLayout() {
        if (weight_number_tv != null) {//避免多次初始化
            return;
        }
        weight_number_tv = (TextView) findViewById(R.id.weight_number_tv);
        weight_number_tv.setText(String.valueOf(currentWeight));

        View weight_iv = findViewById(R.id.weight_iv);

        DisplayMetrics dm = new DisplayMetrics();
        getWindow().getWindowManager().getDefaultDisplay().getMetrics(dm);
        int weightIvPadding = dm.widthPixels / 2 - DensityUtil.dip2px(getContext(), 46);
        weight_iv.setPadding(weightIvPadding, 0, weightIvPadding, 0);
        weight_hsv = (MyHorizontalScrollView) findViewById(R.id.weight_hsv);
        weight_hsv.post(new Runnable() {
            @Override
            public void run() {
                weight_hsv.scrollTo((int) (cell * (currentWeight - 5) + 0.5f), 0);
                weight_hsv.setScrollChangedListener(new MyHorizontalScrollView.ScrollViewListener() {
                    @Override
                    public void onScrollChanged(MyHorizontalScrollView scrollView, int x,
                                                int y, int oldx, int oldy) {
                        if (x != oldx) {
                            currentWeight = convertToWeight(x);
                            weight_number_tv.setText(String.valueOf(currentWeight));
                        }
                    }
                });
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.dialog_input_height_weight);

        setDialogAttributes();
        initDialogView();
    }

    public void setDialogAttributes() {
        Window dialogWindow = getWindow();
        setCancelable(false);
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        DisplayMetrics dm = new DisplayMetrics();
        dialogWindow.getWindowManager().getDefaultDisplay().getMetrics(dm);

        lp.width = dm.widthPixels - DensityUtil.dip2px(getContext(), 30); // 宽度
        // lp.alpha = 0.9f; // 透明度

        dialogWindow.setAttributes(lp);
        dialogWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    }

    /**
     * 相对单位转绝对像素
     *
     * @param source
     * @return
     */
    private float transfer(float source) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return source * scale;
    }

    private int convertToHeight(int y) {

        return 200 - (int) (y / cell + 0.5f);
    }

    private int convertToWeight(int x) {
        return (int) (x / cell + 0.5f) + 5;
    }

}
